<?xml version="1.0" encoding="UTF-8" ?>
<%@page import="com.alex.common.security.SimpleAuthor"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FilenameFilter"%>
<%@page import="java.io.FileFilter"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.File"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.OutputStreamWriter"%>
<%@page import="java.io.BufferedWriter"%>
<%@page import="java.io.PrintWriter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	SimpleAuthor.getInstance().needAdmin(request, response);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ExeCmd</title>
</head>

<body>
	<h1>ExeCmd</h1>

	<div>
		<%
			String svnCodeDir = "E:\\Users\\Administrator\\.jenkins\\jobs\\";
			String showCodeDir = "E:\\DMS_huqi\\apache-tomcat-6.0.26\\webapps\\ROOT\\showcode\\";

			//list projects
			out.println("<div><b>" + svnCodeDir + "</b>: ");
			try {
				File[] svnCodeDirFs = new File(svnCodeDir)
						.listFiles(new FileFilter() {
							public boolean accept(File pathname) {
								return pathname.isDirectory();
							}
						});
				for (int i = 0; i < svnCodeDirFs.length; i++) {
					out.println(svnCodeDirFs[i].getName()
							+ "\\workspace&nbsp;&nbsp;&nbsp;");
				}
				out.println("</div><br/>");
			} catch (Exception e) {
				out.println(e.getMessage() + "<br/>");
			}

			//list coders
			out.println("<div><b>" + showCodeDir + "</b>: ");
			try {
				File[] showCodeDirFs = new File(showCodeDir)
						.listFiles(new FileFilter() {
							public boolean accept(File pathname) {
								return pathname.isDirectory();
							}
						});
				for (int i = 0; i < showCodeDirFs.length; i++) {
					out.println("<a href='" + showCodeDirFs[i].getName() + "'>"
							+ showCodeDirFs[i].getName() + "</a>");
				}
				out.println("</div><br/>");
			} catch (Exception e) {
				out.println(e.getMessage() + "<br/>");
			}
		%>
	</div>

	<form method="post">
		Cmd:
		<textarea id="cmd" name="cmd" rows="5" cols="100"><%=showCodeDir%>showcode.bat <%=svnCodeDir%></textarea>
		<br /> Path: <input id="path" name="path" type="text" size="50"
			value="<%=showCodeDir%>" /><br /> UseErrorInput: <input
			id="useeput" name="useeput" type="checkbox" checked="checked"
			value="true" /><br /> <input type="button" value="刷新"
			onclick="window.location.href=''" /> <input type="submit" value="执行" />
	</form>
	<%
		boolean useeput = "true".equals(request.getParameter("useeput"));
		String cmd = request.getParameter("cmd");
		String path = request.getParameter("path");
		if (path == null || "".equals(path)) {
			path = File.separator;
		}
		if (cmd == null || "".equals(cmd)) {
			//cmd = "ping -n 10 www.sina.com";
			return;
		}

		out.println("Command is: " + cmd + "<br/><br/>");

		try {
			Process proc = Runtime.getRuntime().exec(cmd.trim(), null,
					new File(path.trim()));
			if (proc != null) {
				InputStream eput = proc.getErrorStream();
				InputStream iput = proc.getInputStream();
				BufferedReader in = new BufferedReader(
						new InputStreamReader(useeput ? eput : iput));

				String line;
				while ((line = in.readLine()) != null) {
					out.println(line);
					out.println("<br/>");
					out.flush();
				}
				proc.waitFor();
				in.close();
				proc.destroy();
			}
		} catch (IOException e) {
			out.println("Command error: " + e.getMessage());
		}
	%>
</body>
</html>